package com.isoftstone.common.service.impl;



import com.isoftstone.common.Page;
import com.isoftstone.common.dao.SysMenuMapper;
import com.isoftstone.common.entity.SysMenu;
import com.isoftstone.common.entity.SysUser;
import com.isoftstone.common.service.ISysMenuService;
import com.isoftstone.util.Const;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SysMenuService implements ISysMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;


    public SysMenu getMenuById(String menuId) throws Exception {
        return sysMenuMapper.getMenuById(menuId);
    }

    public SysMenu getMenuByPermission(String permission) throws Exception {
        return sysMenuMapper.getMenuByPermission(permission);
    }

    public List<SysMenu> getMenulistPage(Page page) throws Exception {
        return sysMenuMapper.getMenulistPage(page);
    }

    public List<SysMenu> getMenuAll() throws Exception {
        return sysMenuMapper.getMenuAll();
    }

    @Override
    public String[] getMenusParentIds(String menuIds[]) {
        return sysMenuMapper.getMenusParentIds(menuIds);
    }

    @Transactional
    public void saveSysMenu(SysMenu menu) throws Exception {
        sysMenuMapper.saveSysMenu(menu);
    }
    @Transactional
    public void updateSysMenu(SysMenu menu) throws Exception {
        sysMenuMapper.updateSysMenu(menu);
    }
    @Transactional
    public void deleteSysMenu(String menuId) throws Exception {
        sysMenuMapper.deleteSysMenu(menuId);
    }

    public List<SysMenu> getMenuByUrl(SysUser sysUser,String menuId) throws Exception {


        List<SysMenu> menuList = null;

          if (sysUser.getType().equals(Const.OperatorType.SUPPERADMIN))  {

              // Y 银行端菜单，S企业端菜单
              String iscomcom = sysUser.getSystem().equals(Const.SystemCode.CODE_BMS)?"Y":"S";
              menuList = sysMenuMapper.getMenuALLUrl(iscomcom);
              for (SysMenu menu : menuList) {
                  menu.setSysMenus(this.getALLsubMenu(menu.getMenuId()));

              }

          } else{
              menuList = sysMenuMapper.getMenuByUrl(sysUser.getUserid(),menuId);
              for (SysMenu menu : menuList) {
                  menu.setSysMenus(this.getMenuByParentIdRoleId(menu.getMenuId(),sysUser.getUserid()));

              }
          }


        return menuList;
    }

    @Override
    public List<SysMenu> getMenuByUserId(String userId) throws Exception {
        return sysMenuMapper.getMenuByUserId(userId);
    }

    @Override
    public List<SysMenu> YgetMenuByUrl(SysUser sysUser, String menuId) throws Exception {
        if(StringUtils.equals(sysUser.getType(), Const.OperatorType.SUPPERADMIN)){
            List<SysMenu> menuList = sysMenuMapper.getMenuByUrlBydmin( menuId);

            for (SysMenu menu : menuList) {
                menu.setSysMenus(this.getMenuByParentIdRoleId(menu.getMenuId(), sysUser.getUserid()));
            }
            return menuList;
        }else {
            List<SysMenu> menuList = sysMenuMapper.YgetMenuByUrl(sysUser.getUserid(), menuId);

            for (SysMenu menu : menuList) {
                menu.setSysMenus(this.getMenuByParentIdRoleId(menu.getMenuId(), sysUser.getUserid()));
            }
            return menuList;
        }

        }

    @Override
    public String getMenuNameByMenuId(String s) throws Exception {
        return sysMenuMapper.getMenuNameByMenuId(s);
    }

    @Override
    public List<SysMenu> getMenuByUserType(String type) throws Exception {
        return sysMenuMapper.getMenuByUserType(type);
    }

    public List<SysMenu> getMenuByRoleId(String roleId) throws Exception {
        return sysMenuMapper.getMenuByRoleId(roleId);
    }

    @Override
    public List<SysMenu> getMenuByParentId(String parentId) throws Exception {
        return sysMenuMapper.getMenuByParentId(parentId);
    }

    public List<SysMenu> getMenuByParentIdRoleId(String parentId,String userId) throws Exception {
        if(StringUtils.equals(userId,Const.BankSuperAdmin)) {//如果是银行端超级管理员
            List<SysMenu> menuList = sysMenuMapper.getMenuByParentIdadmin(parentId, userId);
            for (SysMenu menu : menuList) {
                menu.setSysMenus(this.getMenuByParentIdRoleId(menu.getMenuId(), userId));
            }
            return menuList;
        }else {
            List<SysMenu> menuList = sysMenuMapper.getMenuByParentIdRoleId(parentId, userId);
            for (SysMenu menu : menuList) {
                menu.setSysMenus(this.getMenuByParentIdRoleId(menu.getMenuId(), userId));
            }
            return menuList;
        }
    }


    public List<SysMenu> getALLsubMenu(String parentId) throws Exception {
            List<SysMenu> menuList = sysMenuMapper.getALLsubMenu(parentId);
            for (SysMenu menu : menuList) {
                menu.setSysMenus(this.getALLsubMenu(menu.getMenuId()));
            }
            return menuList;
    }

}
